<?php

class Db_Tables_BjmBotsPendingMsg extends Zend_Db_Table
{
    protected $_name = 'bjm_bots_pending_msg';
	var $from_id;
	var $member_id;
	var $api_token;
	var $contents;
	var $url;
	var $target_url = "http://106.15.42.112:8080/char2?";
	
	var $shortLinkUrl;
	var $taoToken;
	var $pid;
	
	function AddAction()
	{
		if($this->from_id && $this->member_id && $this->contents && $this->url && $this->pid && $this->api_token)
		{
			$row = $this->createRow();
			$row->from_id = $this->from_id;
			$row->member_id = $this->member_id;
			$row->api_token = $this->api_token;
			$row->contents = $this->contents;
			$row->url = $this->url;
			$row->status = 0;
			$row->ctime = date("Y-m-d H:i:s");
			$pid = $row->save();
			
			if($pid && $this->pid)
			{
				//send to websocket
				$mod_ext = new Sys_Extensions();
				$pinfo = $mod_ext->GetPidInfo($this->pid);
				
				if($pinfo['adzoneid'] && $pinfo['siteid'])
				{
					$contents = array(
						'pid' => $pid,
						'adzoneid' => $pinfo['adzoneid'],
						'siteid' => $pinfo['siteid'],
						'msg' => $this->url
					);

					$contents_js = Zend_Json::encode($contents);
					$contents_url = urlencode($contents_js);
					
					$uri = $this->target_url."username=".$this->api_token."&message=".$contents_url;
					$ch = curl_init();
					curl_setopt($ch, CURLOPT_URL, $uri);
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
					$tmpInfo = curl_exec($ch);

					if (curl_errno($ch)) {
						echo 'Errno'.curl_error($ch);
					}
					curl_close($ch);
				}
			}
		}
	}
	
	function SendMsg()
	{
		if($this->pid)
		{
			$row = $this->fetchRow("pid=".intval($this->pid)." and status=0");
			
			if($row)
			{
				$from_id = $row->from_id;
				$contents = $row->contents;
				$row->status = 1;
				$row->updated_time = date("Y-m-d H:i:s");
				$row->save();
				
				if($from_id)
				{
					$mod_bots_to = new Db_Tables_BjmBotsTo();
					$mod_bots_to->from_id = $from_id;
					$to_info = $mod_bots_to->GetToInfo();
					
					if(!empty($to_info))
					{
						$CQ = new Sys_CoolQ();
						
						foreach($to_info as $ti)
						{
							if(1 == $ti['bot_type']) //QQ
							{
								if($ti['group_id'] && $contents && $this->shortLinkUrl)
								{
									$contents = preg_replace("/\[\[\]\]/", $this->shortLinkUrl, $contents);
									
//									$CQ->sendGroupMsg($ti['group_id'], $contents);
									
									//cut to to sections
									$cont_ex = explode("\n", $contents);
									
									if(count($cont_ex))
									{
										$content_1 = $cont_ex[0];
										
										unset($cont_ex[0]);
										$content_2 = implode("\n", $cont_ex);
										
										$CQ->sendGroupMsg($ti['group_id'], trim($content_1));
										$CQ->sendGroupMsg($ti['group_id'], trim($content_2));
									}
								}
							}elseif(2 == $ti['bot_type']) //Wechat
							{
								//Wait for Andy
								
							}
						}
						
						unset($CQ);
					}
				}
			}
		}
		
		return TRUE;
	}
}